package com.linkmcn.uaa.service.impl;

import com.linkmcn.core.base.CurrentUser;
import com.linkmcn.security.userdetails.MateUserDetailsService;
import com.linkmcn.uaa.cache.UserCache;
import com.linkmcn.uaa.entity.LinkmcnUser;
import com.linkmcn.uaa.entity.LoginInfo;
import com.linkmcn.uaa.entity.User;
import com.linkmcn.uaa.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * 用户详情实现类
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements MateUserDetailsService {

    @Resource
    private UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        LoginInfo loginInfo = userClient.loginWithUsernamePassword(userName);
        if (loginInfo == null || loginInfo.getUser() == null) {
            throw new UsernameNotFoundException("该用户：" + userName + "不存在");
        }
        return getUserDetails(loginInfo);
    }

    @Override
    public UserDetails loadUserByMobile(String mobile) throws UsernameNotFoundException {

        LoginInfo loginInfo = userClient.loginWithCaptcha(mobile);
        if (loginInfo == null) {
            throw new UsernameNotFoundException("该用户：" + mobile + "不存在");
        }
        return getUserDetails(loginInfo);
    }

    @Override
    public UserDetails loadUserBySocial(String openId) throws UsernameNotFoundException {
//        String userName = "admin";
//        UserInfo userInfo = sysUserProvider.getUserByUserName(userName).getData();
//        if (userInfo == null) {
//            throw new UsernameNotFoundException("该用户：" + userName + "不存在");
//        }
//        userInfo.setType(Oauth2Constant.LOGIN_USERNAME_TYPE);
//        userInfo.setUserName(userName);
//        return getUserDetails(userInfo);
        return null;
    }

    /**
     * @param loginInfo
     * @return
     */
    private UserDetails getUserDetails(LoginInfo loginInfo) {
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList("*:*:*");
        User user = loginInfo.getUser();
        //存入缓存
        CurrentUser currentUser = new CurrentUser();
        currentUser.setUserId(user.getId());
        currentUser.setUserId(user.getId());
        currentUser.setPhone(user.getPhone());
        currentUser.setRealName(user.getRealName());
        currentUser.setObjId(user.getObjId());
        currentUser.setObjType(user.getObjType());
        currentUser.setAuditStatus(user.getAuditStatus());
        currentUser.setWwNum(user.getWwNum());
        currentUser.setHeadUrl(user.getHeadUrl());
        currentUser.setEnable(user.getEnable());
        currentUser.setIsRoot(user.getIsRoot());
        Long tenantId = user.getTenantId();
        currentUser.setNickName(user.getNickName());
        currentUser.setRoleId(user.getRoleId());
        currentUser.setAlipayName(user.getAlipayName());
        currentUser.setAlipayNum(user.getAlipayNum());
        currentUser.setIsBindAlipayNum(user.getIsBindAlipayNum());
        if (tenantId == null) {
            user.setTenantId(-1L);
        }
        currentUser.setTenantId(user.getTenantId());
        currentUser.setActualOperatorId(loginInfo.getActualOperatorId() == null ? user.getId() : loginInfo.getActualOperatorId());
        currentUser.setIssueTime(loginInfo.getIssueTime());
        currentUser.setPerms(loginInfo.getPerms());
        currentUser.setPassAudit(loginInfo.isPassAudit());
        //放入临时容器
        UserCache.set(user.getId(), user.getTenantId(), currentUser);
        return new LinkmcnUser(loginInfo.getUser().getUsername(), loginInfo.getUser().getPassword(), true, true, true, true,
                authorities,
                user.getId(), user.getTenantId());
    }

}
